package com.yoo.ons.dialog

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.widget.ViewPager2
import com.gyf.immersionbar.ImmersionBar
import com.yoo.module_base.R
import com.yoo.module_base.base.BaseDialogFragment
import com.yoo.module_base.utils.StatusBarUtil
import com.yoo.module_base.view.CustomVerticalIndicator
import com.yoo.ons.adapter.ProfileInfoImageAdapter
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.databinding.DialogFragmentProfileBannerBinding
import com.yoo.ons.databinding.DialogPictureViewBinding
import com.yoo.ons.function.album.viewmodel.PersonAlbumViewModel
import com.yoo.ons.function.me.viewmodel.MeViewModel
import com.yoo.ons.model.PersonAlbumModel
import com.yoo.ons.model.PictureManagerModel
import com.yoo.ons.util.loadThumbnails
import com.youth.banner.config.IndicatorConfig
import com.youth.banner.util.BannerUtils

/**
 * 个人照片浏览弹窗
 * */
class PictureViewDialogFragment : BaseDialogFragment() {
    private var innerBinding: DialogPictureViewBinding? = null
    private val binding get() = innerBinding!!
    private val personAlbumViewModel by lazy {
        ViewModelProvider(requireActivity())[PersonAlbumViewModel::class.java]
    }
    override fun setWindow(win: Window) {
        win.setWindowAnimations(R.style.DialogAnimation)
        win.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        win.attributes = lp
        win.setGravity(Gravity.BOTTOM)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding = innerBinding
            ?: DialogPictureViewBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
        binding.titleBar.setPadding(0,StatusBarUtil.getStatusBarHeight(activity),0,0)
        binding.titleBar.setBackListener { dismissAllowingStateLoss() }
        binding.titleBar.setMoreListener {
            personAlbumViewModel.getPersonAlbumRemove(object :HttpRequestListener<PictureManagerModel>(){
                override fun addRequestParams(params: HashMap<String, Any>) {
                    personAlbumViewModel.personAlbumLiveData.value?.albumCode?.let { params["albumCode"] = it }
                    params["imageCode"]=arguments!!.getString(VIEW_IMAGE_CODE,"")
                    params["type"]=3
                }

                override fun success(t: PictureManagerModel?) {
                    val value = personAlbumViewModel.personAlbumLiveData.value
                    value?.apply {
                        this.images?.removeAt(arguments!!.getInt(VIEW_POSITION))
                        personAlbumViewModel.personAlbumLiveData.postValue(value)
                        dismissAllowingStateLoss()
                    }
                }

            })
        }
        binding.titleBar.setMoreVisible(true)
        binding.titleBar.setMoreResId(com.yoo.module_res.R.mipmap.img_picture_remove)

    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(this).statusBarDarkFont(false).init()
    }

    private fun initData() {
        personAlbumViewModel.personAlbumLiveData.observe(viewLifecycleOwner) {
            val modelList = it?.images?.filter { managerModel ->
                managerModel.imageCode == requireArguments().getString(VIEW_IMAGE_CODE)
            }
            if (!modelList.isNullOrEmpty()){
                binding.pictureImageView.loadThumbnails(modelList[0].imageUrl,binding.pictureImageView.width)
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        //一定要修改这个值，要不然二次打开就先走observe
        innerBinding = null
        dismissAllowingStateLoss()
    }

    companion object {
        private const val VIEW_POSITION = "View_Position"
        private const val VIEW_IMAGE_CODE = "View_Image_Code"
        fun newInstance(imageCode:String,position: Int): PictureViewDialogFragment {
            return PictureViewDialogFragment().apply {
                arguments = Bundle().apply {
                    putString(VIEW_IMAGE_CODE, imageCode)
                    putInt(VIEW_POSITION, position)
                }
            }
        }
    }
}